var fusion={
	exec:function(method,cmd){
		var ret;
		$.ajax({
			   type:method,
			   url:'https://www.google.com/fusiontables/api/query',
			   data:{
			   sql:cmd
			   },
			   success:function(data){
			   ret=data;
			   }
		   })
		
	}	
}

var database={
	ensureExist:function(){

		//create remote db
		{
			alert(fusion.exec('POST','CREATE TABLE if not exists trans(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, time DATETIME, categoryId INTEGER, amount INTEGER, title TEXT, ps TEXT);'))
		}
		
		
		//create local db
		
		var db=openDatabase('smtm','1.0','show-me-the-money',10*1024*1024)
		
		var errorHandler=function (transaction, error){
			alert('Oops. Error was '+error.message+' (Code '+error.code+')');
			// Handle errors here
			var we_think_this_error_is_fatal = true;
			if (we_think_this_error_is_fatal) return true;
			return false;
		}
		
		db.transaction(function(transaction) 
		{
			transaction.executeSql('CREATE TABLE if not exists trans(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, time DATETIME, categoryId INTEGER, amount INTEGER, title TEXT, ps TEXT);')

			transaction.executeSql('insert into trans (time,categoryId,amount,title,ps) VALUES("2011/2/2",0,300,"dinner","no");')
			transaction.executeSql("SELECT * FROM trans;",[],function(transaction, results){
				var string=''
				for (var i=0; i<results.rows.length; i++) {
					var row = results.rows.item(i);
				    string+= row['title'] + '\n';
				}		
			    alert(string)
			},errorHandler)
		})
	},
	recentTransactions:function(iRecords){
		
	}
}





/*
 transaction.executeSql('insert into people (name, shirt) VALUES("Joe", "Green");', [], nullDataHandler, errorHandler);
 transaction.executeSql('insert into people (name, shirt) VALUES("Mark", "Blue");', [], nullDataHandler, errorHandler);
 transaction.executeSql('insert into people (name, shirt) VALUES("Phil", "Orange");', [], nullDataHandler, errorHandler);
 transaction.executeSql('insert into people (name, shirt) VALUES("jdoe", "Purple");', [], nullDataHandler, errorHandler);	
 }	
 
 );
 
 var name = 'jdoe';
 var shirt = 'fuschia';
 db.transaction(
 function (transaction) {
 transaction.executeSql("UPDATE people set shirt=? where name=?;",[ shirt, name ]); // array of values for the ? placeholders
 }
 );
 
 
 
 
 function dataHandler(transaction, results){
 // Handle the results
 var string = "Green shirt list contains the following people:\n\n";
 for (var i=0; i<results.rows.length; i++) {
 var row = results.rows.item(i);
 string = string + row['name'] + '\n';
 }		
 alert(string);
 }
 
 db.transaction(
 function (transaction) {
 alert("this is the transation"  +transaction)
 alert("this is the db   "+db)
 //	transaction.executeSql("SELECT * from people where shirt='Green';",	[],dataHandler, errorHandler);	
 transaction.executeSql("SELECT name FROM people where shirt='Green';",	[],dataHandler, errorHandler);
 //SELECT LastName,FirstName FROM Persons
 }
 );	
 
 
 
 }*/
